from google.appengine.api import users
from google.appengine.ext import db
from common import *
import datetime

string_encoding = 'latin1'

class Dude(db.Model):
    email = db.StringProperty()
    nickname = db.StringProperty()
    homepage = db.StringProperty()
    created_on = db.DateTimeProperty(auto_now_add = 1)
    created_by = db.UserProperty()
    comments = db.StringProperty(multiline=True)
    standard_email_sign = db.StringProperty(multiline=True)
    # new
    recieve_newsletter = db.BooleanProperty(default = True)
    location = db.StringProperty()
    enable_fancy_stuff = db.BooleanProperty(default = True)

    def homepage_(self):
        return self.homepage.encode(string_encoding)

    def nickname_(self):
        return self.nickname.encode(string_encoding)

    def comments_(self):
        return self.comments.encode(string_encoding)

    def location_(self):
        return self.location.encode(string_encoding)

    def standard_email_sign_(self):
        return self.standard_email_sign.encode(string_encoding)

    def get_absolute_url(self):
        return '/dude/%s/' % self.key()
        
    def has_access(self):
        return str(users.get_current_user().email()) in [str(acl.owner.email) for acl in self.guests]

class Movie(db.Model):
    title = db.StringProperty()
    buy_url = db.StringProperty()
    imdb_url = db.StringProperty()
    comments = db.StringProperty()
    created_on = db.DateTimeProperty(auto_now_add = 1)
    created_by = db.UserProperty()
    media_type = db.IntegerProperty(default = 0)
    status = db.IntegerProperty(default = 0)
    genre = db.IntegerProperty(default = 0)
    
    kudos_count = db.IntegerProperty(default = 0)
    
    can_be_sold = db.BooleanProperty(default = True)
    suggested_price = db.StringProperty()
    can_be_borrowed = db.BooleanProperty(default = True)
    can_be_swapped = db.BooleanProperty(default = True)
    is_public = db.BooleanProperty(default = False)
    
    def comments_count(self):
        return len(list(self.user_comments))
    
    def for_sale_only(self):
        if self.can_be_borrowed or self.can_be_swapped:
            return False
        return self.can_be_sold == True
        
    def title_(self):
        return self.title.encode(string_encoding)

    def comments_(self):
        return self.comments.encode(string_encoding)

    def imdb_url_(self):
        return self.imdb_url.encode(string_encoding)

    def buy_url_(self):
        return self.buy_url.encode(string_encoding)
    
    def get_absolute_url(self):
        return '/movie/%s/' % self.key()

    def status_text(self):
        return status_dict.get(self.status, '')
        
    def media_text(self):
        return media_dict.get(self.media_type, '?')
        
    def imdb_qs(self):
        if self.imdb_url:
            return self.imdb_url_()
        return 'http://www.imdb.com/find?s=all&q=%s' % '+'.join(self.title_.lower().split(' '))
        
    def contact_mail(self):
        return self.created_by.email()
    
    def genre_text(self):
        return genre_dict.get(self.genre, '')
        
    def review_mode(self):
        return self.status == MovieStatus.Review

    def calculated_rating(self):
        if not self.ratings:
            return None
        
        value = 0.0
        counter = 0.0
        for r in self.ratings:
            value += r.rating
            counter += 1
        if counter != 0:
            return '%.1f' % (value / counter)
        return None

    def is_owner(self):
        return self.created_by == users.get_current_user()
        
    def is_available(self):
        return self.status == MovieStatus.Available
    
    def owner_nickname(self):
        query = db.GqlQuery("SELECT * FROM Dude WHERE email = :1", self.created_by.email())
        qs = query.fetch(1)
        dude = None
        if len(qs) == 1:
            dude = qs[0]
        return dude.nickname.encode(string_encoding)

    def can_give_kudos(self):
        current_user = users.get_current_user()
        return current_user not in [i.user for i in self.kudos] and self.created_by != current_user

class Access(db.Model):
    owner = db.ReferenceProperty(Dude, collection_name="owners")
    guest = db.ReferenceProperty(Dude, collection_name="guests")
    
class Kudos(db.Model):
    user = db.UserProperty()
    movie = db.ReferenceProperty(Movie, collection_name="kudos")

class Action(db.Model):
    user = db.UserProperty()
    movie = db.ReferenceProperty(Movie, collection_name="actions")
    action = db.StringProperty()
    completed = db.BooleanProperty(default = None)
    
class Rating(db.Model):
    user = db.UserProperty()
    movie = db.ReferenceProperty(Movie, collection_name="ratings")
    rating = db.IntegerProperty(default = 0)

class Loan(db.Model):
    guest = db.ReferenceProperty(Dude, collection_name="loaners")
    movie = db.ReferenceProperty(Movie, collection_name="loans")
    created_on = db.DateTimeProperty(auto_now_add = 1)
    due_date = db.DateTimeProperty()
    returned = db.BooleanProperty(default = None)
    
    def over_due(self):
        return datetime.datetime.today() < self.due_date

class MovieComment(db.Model):
    movie = db.ReferenceProperty(Movie, collection_name="user_comments")
    created_on = db.DateTimeProperty(auto_now_add = 1)
    author = db.ReferenceProperty(Dude, collection_name="comment_authors")
    comment = db.StringProperty()
    
    def comment_(self):
        return self.comment.encode(string_encoding)

class SystemInfo(db.Model):
    date_of_last_newsletter = db.DateTimeProperty(auto_now_add = 1)
    
class MiniBlog(db.Model):
    title_ = db.StringProperty()
    body_ = db.StringProperty()
    created_on = db.DateTimeProperty(auto_now_add = 1)
    created_by = db.UserProperty()
    sort_order = db.IntegerProperty(default = 999)
    public = db.BooleanProperty(default = True)
    
    def get_title(self):
        return self.title_.encode('latin1')
        
    def set_title(self, value):
        self.title_ = unicode(value, 'utf-8')

    title = property(get_title, set_title)